<?php
namespace Seller\Controller;
use Think\Controller;
class LoginController extends Controller 
{
    /*登录页面*/
    public function index()
    {
		 $code = $_GET['code'];//获取code
        //var_dump($code);exit();
        if(!empty($code))
        {	
			//授权的
            $appModer = M('App');
            $res       = $appModer->select();
            $appid = trim($res[0]['appid']);
            $secret = trim($res[0]['appsecret']);
			
			//网页授权获取用户openid Start
			$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
			$weixin     = file_get_contents($url);//通过code换取网页授权access_token		   
			$jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
			$array      = get_object_vars($jsondecode);//转换成数组
			$openid     = $jsondecode->openid;//输出openid
			//网页授权获取用户openid End
			//var_dump($openid);exit;
			//写入openid

			
 			$arr = unserialize(F('data.php'));
			$nowTime = time();
			if($nowTime-$array['shijian']>3600 || $array['shijian'] == '')
			{
				//获取获取用户信息的token Start
				$shijian = time();
				$get_token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid .'&secret='.$secret;
				$get_token_json = file_get_contents($get_token_url);
				$get_token_obj = json_decode($get_token_json);
				$get_token_array = get_object_vars($get_token_obj);
				//var_dump($get_token_array);die;
				$token = $get_token_array['access_token'];
				//var_dump($token);die;
				$data = array(
					'token' => $token,
					'time' => time(),
				);
				//获取获取用户信息的token End
				F('data.php',$data);
				
			}else{
				$token = $arr['token'];
			} 
			
			//获取信息的
			
			
			
            $url1 = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$token.'&openid='.$openid.'&lang=zh_CN';
			
 			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url1);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 要求结果为字符串且输出到屏幕上
			curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率
			curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
			curl_setopt($ch, CURLOPT_TIMEOUT, 15);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
			$output = curl_exec($ch);
			curl_close($ch);
			$jsondecode1 = json_decode($output); 
			$array1 = get_object_vars($jsondecode1);
            //var_dump($array1);exit;		
			$array1['nickname'] = preg_replace('/\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]/', '', $array1['nickname']);
			
			$Model = new \Think\Model();
			$new_point = $Model->execute("update shop set head_pic= '".$array1['headimgurl']."', nickname ='".$array1['nickname']."', openid ='".$openid."' where shop_id = ".session('shop_id'));
						
			$shopModel    = M('shop');
            $shop['shop_id'] = session('shop_id');
            $res          = $shopModel->where($shop)->limit(1)->select();

            if($res)
            {
				session('shop_id',$res['0']['shop_id']);
                session('openid',$res['0']['openid']);
				$this->redirect('/Seller/Index/index');
                //$this->success('您已登录！', __APP__.'/Seller/Index/index');
           }
        }else{
			session('shop_id', 0);
			session('openid', 0);
			session_destroy();			
		}
       //$this->assign('openid', $openid);

       $this->display();
		
    }

    /*登录*/
    public function signIn()
    {
    	$arr['phone'] = trim(I('post.phone'));
    	$arr['pwd']	  = md5(I('post.pwd'));
		$arr['isApply'] = 1;
		$arr['apply'] = 1;

    	$ShopModer    = D('Shop');
    	$res          = $ShopModer->checkUse($arr['phone'], $arr['pwd']);

    	if($res == '1')
    	{
            $shopModer    = M('Shop');
            $shopRes          = $shopModer->where($arr)->limit(1)->select();
            
            $points = $ShopModer->points($shopRes[0]['shop_id']);
			session('shop_id',$shopRes[0]['shop_id']);
    		$this->success('登录成功！', __APP__.'/Seller/Index/index');

    		

    	}elseif($res == 'no')
    	{
    		$this -> error('你的店铺信息还未审核！');
    	}elseif($res == 'notdo')
        {
            $this -> error('你输入的账号密码不正确！');
        }else
        {
            $this -> error('输入的电话号码不正确！');
        }
    }

    /*注册*/
    public function register()
    {
        $areaModel = D('area');
        $areaRes = $areaModel -> index('area');

        $classModel = D('class');
        $classRes = $classModel -> index();

        $this->assign('areaRes', $areaRes);
        $this->assign('classRes', $classRes);
        $this->display();
    }

    /*商家推广注册*/
    public function register1()
    {
        $areaModel = D('area');
        $areaRes = $areaModel -> index('area');

        $classModel = D('class');
        $classRes = $classModel -> index();


        $this->assign('tsid',$_GET['sid']);
        $this->assign('areaRes', $areaRes);
        $this->assign('classRes', $classRes);
        $this->display();
    }
	
	/*会员推广注册*/
    public function register2()
    {
        $areaModel = D('area');
        $areaRes = $areaModel -> index('area');

        $classModel = D('class');
        $classRes = $classModel -> index();


        $this->assign('tuid',$_GET['uid']);
        $this->assign('areaRes', $areaRes);
        $this->assign('classRes', $classRes);
        $this->display();
    }
	
	//直接注册
    public function registerIn()
    {
        $arr['name'] = trim(I('post.name'));
        $arr['contacts']   = trim(I('post.contacts'));
        $arr['phone'] = trim(I('post.phone'));
        $arr['pwd']   = md5(I('post.pwd'));
        $arr['area'] = trim(I('post.area'));
        $arr['type']   = trim(I('post.type'));  
        $arr['address'] = trim(I('post.address'));
        $arr['synopsis']   = trim(I('post.synopsis'));
        $arr['card']   = '';
        $arr['banktype']   = '';
        $arr['leverPhone'] = trim(I('post.leverPhone'));
        $arr['leverType'] = trim(I('post.leverType'));

		$have_shop = M('shop') -> where(array('phone'=>$arr['phone'],'apply'=>0,'isApply'=>1)) -> count();
		if($have_shop){
			$this -> error('商家账号已经存在!', 'register');
			exit();
		}
        /*$shopModel = D('shop');
        $path = $shopModel->upload();
        //var_dump($path);exit();
        if($path)
        {
            $arr['photo1']   = $path['photo1']['savepath'].$path['photo1']['savename'];
        }else
        {
            $this -> error('图片保存失败', 'register');
        }*/

        $ShopModer    = D('Shop');
        $res          = $ShopModer->insertShop($arr);

        if ($res == 'data')
        {
            $this->success('注册成功，请等待审核','index');
            
        }elseif ($res == 'udata')
        {
            $this->success('注册成功，请等待审核','index');
            
        }elseif ($res == 'sdata')
        {
            $this->success('注册成功，请等待审核','index');
            
        }elseif ($res == 'noLever')
        {
            $this -> error('您输入的上级不存在,请核对!', 'register');
            
        }elseif ($res == 'err')
        {
            $this -> error('您填写的电话号码格式不正确!', 'register');
            
        }elseif ($res == 'have')
        {
            $this -> error('输入的电话号码已被注册!', 'register');
            
        }
    }
    //商家推广以后注册
    public function registerIn1()
    {
        $arr['name'] = trim(I('post.name'));
        $arr['contacts']   = trim(I('post.contacts'));
        $arr['phone'] = trim(I('post.phone'));
        $arr['pwd']   = md5(I('post.pwd'));
        $arr['area'] = trim(I('post.area'));
        $arr['type']   = trim(I('post.type'));
        $arr['pid'] = trim(I('post.tsid'));
        $arr['is_user'] = 0;
        $arr['address'] = trim(I('post.address'));
        $arr['synopsis']   = trim(I('post.synopsis'));
        $arr['card']   = '';
        $arr['banktype']   = '';
        
        $shopModel = D('shop');
        $path = $shopModel->upload();
        //var_dump($path);exit();
        if($path)
        {
            $arr['photo1']   = $path['photo1']['savepath'].$path['photo1']['savename'];
        }else
        {
            $this -> error('图片保存失败', 'register1');
        }

        $ShopModer    = D('Shop');
        $res          = $ShopModer->insertShop1($arr);

        if ($res)
        {
            $this->success('注册成功，请等待审核','watch');
            
        }elseif ($res == 0)
        {
            $this -> error('输入的电话号码已被注册</br>或者您填写的电话号码格式不正确', 'register1');
            
        }
    }
	 //会员推广商家,以后注册
    public function registerIn2()
    {
        $arr['name'] = trim(I('post.name'));
        $arr['contacts']   = trim(I('post.contacts'));
        $arr['phone'] = trim(I('post.phone'));
        $arr['pwd']   = md5(I('post.pwd'));
        $arr['area'] = trim(I('post.area'));
        $arr['type']   = trim(I('post.type'));
        $arr['pid'] = trim(I('post.tuid'));
        $arr['is_user'] = 1;
        $arr['address'] = trim(I('post.address'));
        $arr['synopsis']   = trim(I('post.synopsis'));
        $arr['card']   = '';
        $arr['banktype']   = '';
        
        $shopModel = D('shop');
        $path = $shopModel->upload();
        //var_dump($path);exit();
        if($path)
        {
            $arr['photo1']   = $path['photo1']['savepath'].$path['photo1']['savename'];
        }else
        {
            $this -> error('图片保存失败', 'register2');
        }

        $ShopModer    = D('Shop');
        $res          = $ShopModer->insertShop2($arr);

        if ($res)
        {
            $this->success('注册成功，请等待审核','watch');
            
        }elseif ($res == 0)
        {
            $this -> error('输入的电话号码已被注册</br>或者您填写的电话号码格式不正确', 'register2');
            
        }
    }
	/*公众号关注*/
    public function watch()
    {
        $this->display();
    }
}